package sanrenxing.dayEnd.backRepertoryTotal.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sanrenxing.dayEnd.backRepertoryTotal.domain.ShowValuation;
import sanrenxing.dayEnd.backRepertoryTotal.domain.Valuation;
import sanrenxing.dayEnd.backRepertoryTotal.service.ValuationService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @desrciption 资产估值
 * @auth：yidu 黄雄珠
 * @date:2021/3/13 0013 - 9:48
 * @version:1:0
 */
@RestController
@RequestMapping("/valuation")
public class ValuationController {

    @Autowired
    private ValuationService valuationService;

    /**
     *
     * @param request
     * @return
     */
    @PostMapping("/method")
    public String valuationMethod(HttpServletRequest request){
        HttpSession session = request.getSession();
        //判断session当中保存的是否为当天日期，如果是当天日期则不进行估值，因为已经估值了
        if (session.getAttribute("valuationData")!=null && session.getAttribute("valuationData").equals(new SimpleDateFormat("yyyy-MM-dd").format(new Date()))){
            return "already";
        }else {
            try {
                List<Valuation> valuations = valuationService.queryValua();
                for (Valuation valuation : valuations) {
                    valuationService.addValuation(valuation);
                }
                session.setAttribute("valuationData",new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
                return "success";
            }catch(Exception e){
                e.printStackTrace();
                return "fail";
            }
        }
    }

    @GetMapping("/method")
    public Map<String,Object> queryValuation(HttpServletRequest request){
        HttpSession session = request.getSession();
        List<ShowValuation> list=new ArrayList<>();
        ShowValuation showValuation = new ShowValuation();
        ShowValuation showValuation1 = new ShowValuation();
        showValuation.setBusinType("证券增值估值");
        showValuation1.setBusinType("清算款");
        //判断当天已经估值了
        if (session.getAttribute("valuationData")!=null && session.getAttribute("valuationData").equals(new SimpleDateFormat("yyyy-MM-dd").format(new Date()))){
            showValuation.setState(1);
            showValuation1.setState(1);
        }else {//当天资产未估值
            showValuation.setState(0);
            showValuation1.setState(0);
        }
        list.add(showValuation1);
        list.add(showValuation);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",2);
        map.put("data",list);
        return map;
    }

}
